package com.sunday.shoufen.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sunday.common.base.BaseActivity;
import com.sunday.common.model.ResultDO;
import com.sunday.common.utils.Constants;
import com.sunday.common.utils.SharePerferenceUtils;
import com.sunday.common.utils.StringUtils;
import com.sunday.common.utils.ToastUtils;
import com.sunday.common.widgets.ClearEditText;
import com.sunday.shoufen.R;
import com.sunday.shoufen.base.BaseApp;
import com.sunday.shoufen.entity.Member;
import com.sunday.shoufen.http.ApiClient;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by 刘涛 on 2017/10/30.
 */

public class LoginActivity extends BaseActivity {


    @Bind(R.id.left_btn)
    ImageView leftBtn;
    @Bind(R.id.title_view)
    TextView titleView;
    @Bind(R.id.rightTxt)
    TextView rightTxt;
    @Bind(R.id.right_btn)
    ImageView rightBtn;
    @Bind(R.id.common_header)
    RelativeLayout commonHeader;
    @Bind(R.id.edit_mobile)
    ClearEditText editMobile;
    @Bind(R.id.edit_pwd)
    ClearEditText editPwd;
    @Bind(R.id.btn_login)
    TextView btnLogin;
    @Bind(R.id.btn_forget_pwd)
    TextView btnForgetPwd;
    @Bind(R.id.btn_mobile_login)
    TextView btnMobileLogin;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        titleView.setText("登录");
        leftBtn.setVisibility(View.GONE);
        rightTxt.setTextColor(ContextCompat.getColor(mContext, R.color.main_color));
        rightTxt.setText("注册");
    }

    @OnClick({R.id.rightTxt, R.id.btn_login, R.id.btn_forget_pwd,R.id.btn_mobile_login})
    void onClick(View v) {
        switch (v.getId()) {
            case R.id.rightTxt:
                intent = new Intent(mContext, RegisterActivity.class);
                startActivity(intent);
                break;
            case R.id.btn_login:
                String mobile1 = editMobile.getText().toString().trim();
                if (TextUtils.isEmpty(mobile1) || !StringUtils.isMobileNO(mobile1)) {
                    ToastUtils.showToast(mContext, "请输入正确的手机号");
                    return;
                }
                String pwd = editPwd.getText().toString().trim();
                if (TextUtils.isEmpty(pwd)) {
                    ToastUtils.showToast(mContext, "请输入密码");
                    return;
                }
                Call<ResultDO<Member>> call = ApiClient.getApiAdapter().login(mobile1, pwd);
                call.enqueue(new Callback<ResultDO<Member>>() {
                    @Override
                    public void onResponse(Call<ResultDO<Member>> call, Response<ResultDO<Member>> response) {
                        if (isFinish || response.body() == null) {
                            return;
                        }
                        ResultDO<Member> resultDO = response.body();
                        if (response.body().getCode() == 0) {
                            SharePerferenceUtils.getIns(mContext).saveOAuth(resultDO.getResult());
                            SharePerferenceUtils.getIns(mContext).putBoolean(Constants.IS_LOGIN, true);
                            Member member = response.body().getResult();
                            BaseApp.getInstance().setMember(member);
                            intent = new Intent(mContext, MainActivity.class);
                            startActivity(intent);
                            finish();
                        } else {
                            ToastUtils.showToast(mContext, resultDO.getMessage());
                        }
                    }

                    @Override
                    public void onFailure(Call<ResultDO<Member>> call, Throwable t) {
                        ToastUtils.showToast(mContext, R.string.network_error);
                    }
                });
                break;
            case R.id.btn_forget_pwd:
                intent = new Intent(mContext, RegisterActivity.class);
                intent.putExtra("type", 2);
                startActivity(intent);
                break;
            case R.id.btn_mobile_login:
                intent = new Intent(mContext, MobileLoginActivity.class);
                startActivity(intent);
                break;
        }
    }
}
